<?php

namespace App\Models;

use houdunwang\arr\Arr;
use Illuminate\Database\Eloquent\Model;

class Attr extends Model
{
    protected $guarded = [];

    // 自关联获取父类数据
    public function attr()
    {
        return $this->belongsTo(Attr::class, 'pid', 'id');
    }

    // 判断是否有子类
    public function hasChild($model)
    {
        // 获取所有属性数据
        $data = Attr::get()->toArray();
//        dd($model->id);
        return Arr::hasChild($data, $model['id'], 'pid');
    }

    // 获取子属性
    public function attrSon(){
        return $this->hasMany(Attr::class,'pid','id');
    }
}
